#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :guotie_jd_main.py
# Time  :2024/12/23 16:17
# Email :fortunatedong@163.com
# Author:by Adonli
import json
import random
import sys
import time

import toml
from playwright.sync_api import sync_playwright
from dingding_guotie_jd.utils.schedule_task_work import ScheduledTask
from jingdong_shop import GetJingDongPrice
from dingding_guotie_jd.dingding_robot.messages_push import MessagePush
from dingding_guotie_jd.settings.logger import Loggings
logs = Loggings()

def get_jingdong_keywords_price(shop,keywords):
    cfg = toml.load('settings/config.toml')
    jingdong_skus_price_list_all = []
    shopid_list = []
    if "理光" in shop or "RICOH" in shop or "ricoh" in shop or "Ricoh" in shop:
        shopid_list = cfg["SHOP"]["LIGUANG"]
    elif "欣彩" in shop or "Anycolor" in shop or "ANYCOLOR" in shop or "anycolor" in shop:
        shopid_list = cfg["SHOP"]["XINCAI"]
    elif "惠普" in shop or "HP" in shop or "Hp" in shop or "hp" in shop:
        shopid_list = cfg["SHOP"]["HP"]
    for i in shopid_list:
        with sync_playwright() as pw:
            jingdong_skus_price_list = GetJingDongPrice(pw,i["shop_id"],i["shop_name"],keywords).get_jingdong_sku_price()
            for j in jingdong_skus_price_list:
                jingdong_skus_price_list_all.append(j)
        time.sleep(random.randint(1,3))
    return jingdong_skus_price_list_all

def read_guotie_skes():
    with open('data/guotie_skus_detail.json', 'r') as f:
        guotie_skus = f.read()
    return guotie_skus

def get_keywords():
    guotie_skus = read_guotie_skes()
    guotie_skus_dict = json.loads(guotie_skus)
    # print(guotie_skus_dict["sku"])
    for i in guotie_skus_dict["sku"]:

        # keyword = i["sku_model_code"]+" "+i["sku_fcname"]
        keyword = i["sku_model_code"]
        sku_brand_name = i["sku_brand_name"]
        sku_sell_price = i["sku_sell_price"]
        sku_id = i["sku_id"]
        sku_name = i["sku_name"]
        start_time = time.time()
        logs.info(f"************开始查询************")
        logs.info(f"国铁商品SKU：{sku_id}；查询型号关键词：{keyword}；国铁商品名称：{sku_name}")
        jingdong_skus_price_list_all = get_jingdong_keywords_price(sku_brand_name,keyword)
        search_jingdong_price = {
            "guotie_sku_brand_name":sku_brand_name,
            "guotie_skus_price":sku_sell_price,
            "guotie_sku_name":sku_name,
            "guotie_sku_model_code":i["sku_model_code"],
            "guotie_sku_id":sku_id,
            "guotie_keyword":keyword,
            "search_jingdong_price":jingdong_skus_price_list_all
        }
        MessagePush(search_jingdong_price).success_text_push()
        logs.info(f"已发送钉钉通知")
        time.sleep(random.randint(1, 3))
        end_time = time.time()
        logs.info(f"************结束查询：查询{sku_id}耗时{end_time-start_time}秒************")

if __name__ == '__main__':
        scheduled_task = ScheduledTask(task=get_keywords, start_time="8:30", Loop=True, duration="4h", end_time="22:00",
                                       cycle="day")
        scheduled_task.start()

